package be.jprojects.subscription.gui.detailpanels;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import be.jprojects.subscription.controllers.ListenersController;
import be.jprojects.subscription.gui.crudstatus.CRUDStatus;
import be.jprojects.subscription.gui.crudstatus.CreationStatus;
import be.jprojects.subscription.gui.crudstatus.DeletionStatus;
import be.jprojects.subscription.gui.crudstatus.ReadStatus;
import be.jprojects.subscription.gui.crudstatus.UpdateStatus;
import be.jprojects.subscription.gui.listeners.CRUDActionListener;

public abstract class DetailPanel extends JPanel implements CRUDActionListener {
	private CRUDStatus creationStatus, readStatus, updateStatus, deletionStatus;
	public CRUDStatus getCreationStatus() {
		return creationStatus;
	}

	public CRUDStatus getReadStatus() {
		return readStatus;
	}

	public CRUDStatus getUpdateStatus() {
		return updateStatus;
	}

	public CRUDStatus getDeletionStatus() {
		return deletionStatus;
	}

	private CRUDStatus currentStatus;
	
	public DetailPanel(String name) {
		super.setName(name);
		super.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder(name),
                BorderFactory.createEmptyBorder(5,5,5,5)));
		creationStatus = new CreationStatus(this);
		readStatus = new ReadStatus(this);
		updateStatus = new UpdateStatus(this);
		deletionStatus = new DeletionStatus(this);
		currentStatus = readStatus;
		

		ListenersController.getInstance().addCRUDActionListener(super.getName(), this);
	}
	
	public void deleteActionPerformed() {
		currentStatus = deletionStatus;
		enableAll(true);
	}
	public void newActionPerformed() {
		currentStatus = creationStatus;
		clearAll();
		enableAll(true);
	}
	public void updateActionPerformed() {
		currentStatus = updateStatus;
		enableAll(true);
	}

	public void saveActionPerformed() {
		currentStatus.saveActionPerformed();
		enableAll(false);
	}
	
	public void cancelActionPerformed() {
		currentStatus.cancelActionPerformed();
		enableAll(false);
	}
	
	public abstract void saveCreationActionPerformed();
	public abstract void saveDeletionActionPerformed();
	public abstract void saveUpdateActionPerformed();
	public abstract void cancelCreationActionPerformed();
	public abstract void cancelDeletionActionPerformed();
	public abstract void cancelUpdateActionPerformed();
	
	public abstract void enableAll(boolean enableAll);
	public abstract void clearAll();
	
	public CRUDStatus getCurrentStatus() {
		return currentStatus;
	}

	public void setCurrentStatus(CRUDStatus currentStatus) {
		this.currentStatus = currentStatus;
	}
}
